const express = require('express')
const multer = require('multer');
const dynamic = express.Router()
const fs = require("fs");
const path=require('path')
const img = multer({dest:__dirname +'/img'})
dynamic.use(img.any()) 
const {getDynamic,postDynamic,postAddDynamic,getDynamicID,postUpdateDynamic,postDeleteDynamic} = require('../../service/dynamic/dynamicService') 

//获取艺术涂料数据
dynamic.get('/',getDynamic)
dynamic.post('/',postDynamic)

//id查询
dynamic.get('/id',getDynamicID)

//添加艺术涂料数据
dynamic.post('/add',postAddDynamic)

//修改艺术涂料数据
dynamic.post('/update',postUpdateDynamic)

//删除艺术涂料数据
dynamic.post('/delete',postDeleteDynamic)
//添加图片
dynamic.post('/img',async(req,res,next) =>{
  const files = req.files
  let oldfile = req.files[0].path
  let newfile = req.files[0].path +path.parse(req.files[0].originalname).ext
  fs.renameSync(oldfile,newfile)
  res.send({
    err:0,
    msg:'上传文件成功',
   
    // url:"http://127.0.0.1:3000/imgDynamic/"+req.files[0].filename+path.parse(req.files[0].originalname).ext
    url:"http://47.119.123.95:3000/imgDynamic/"+req.files[0].filename+path.parse(req.files[0].originalname).ext
  })
})


module.exports = dynamic